<?php

/**
 * O2O团购PC版本
 * ============================================================================
 * * 版权所有 2009-2016 郑州司科计算机科技有限公司，并保留所有权利。
 * 购买网址: http://www.jzphp.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
  * 咨询电话：400-823-0371 QQ：313999875
 * ============================================================================
 * $Author: zzsike $
 * $Time 2016-02-03 sammy $
*/


require_once(dirname(dirname(__FILE__)) . '/app.php');
require_once(dirname(__FILE__) . '/paybank.php');
$id = intval($_GET['id']);
$order = Table::Fetch('order', $id);

if($_GET['origin'])
	$total_origin=(real)$_GET['origin'];
	
//print_r($order);
if (!$order) { 
	Session::Set('error', '订单不存在');
	redirect( WEB_ROOT . '/index.php' );
}
if ( $order['user_id'] != $login_user['id']) {
	redirect( WEB_ROOT . "/team.php?id={$order['team_id']}");
}


//如果购物车有多件商品
if(is_user_edit() == true){
	$condition = array( 
			 "packageid = $id or id = $id",
			 'state'=>'unpay',
			);
	$order_list = DB::LimitQuery('order', array(
		'condition' => $condition,
	));
}
$team_num = count($order_list);
function get_team_title($id){
   $team = Table::Fetch('team', $id);
   return $team['title'];
}

foreach($order_list as $k=>$v){
	$team = Table::Fetch('team', $v['team_id']);
	$team['state'] = team_state($team);
	if ( $team['close_time'] ) {
		redirect( WEB_ROOT . "/team.php?id={$id}");
	}
}
$total_origin=$order['origin'];
if ( $order['state'] == 'unpay' ) {

	/* payservice choice */
	if(@$INI[$order['service']]['mid'] || @$INI[$order['service']]['acc']) {
		$ordercheck[$order['service']] = 'checked';
	}
	else {
		foreach($payservice AS $pone) {
			if(@$INI[$order['service']]['mid'] || @$INI[$order['service']]['acc']) { $ordercheck[$order['service']] = 'checked'; }
		}
	}
	$credityes = ($login_user['money'] >= $total_origin);
	$creditonly = ($team['team_type'] == 'seconds' && option_yes('creditseconds'));
}
//print_r($order_list);

if($order_list){
	
	$total_origin=0;
	foreach($order_list as $k=>$v){
		if ( $v['state'] == 'unpay' ) {
			
			if (! ($v['pay_id'] 
						&& (preg_match('#-(\d+)-(\d+)-#', $v['pay_id'], $m) 
							&& ( $m[1] == $order['id'] && $m[2] == $v['quantity']) )
				  )) {
				$randid = strtolower(Utility::GenSecret(4, Utility::CHAR_WORD));
				$pay_id = "go-{$v['id']}-{$v['quantity']}-{$randid}";
				Table::UpdateCache('order', $v['id'], array(
							'pay_id' => $pay_id,
							));
				}
				
			}	
			$total_origin +=$v['origin'];
		}
		
	$credityes = ($login_user['money'] >= $total_origin);
	
	die(include template('order_check'));
}



redirect( WEB_ROOT . "/order/view.php?id={$id}");
